package crp.core.security.domain.services;

import com.qwlabs.panache.Ranged;
import crp.core.security.domain.models.RoleEntity;
import crp.core.security.domain.models.RoleQuery;
import crp.core.security.domain.models.UserRoleEntity;
import crp.core.security.domain.repositories.RoleRepository;
import crp.core.security.domain.repositories.UserRoleRepository;
import crp.core.shared.cache.CacheNames;
import io.quarkus.cache.CacheInvalidateAll;
import io.quarkus.cache.CacheResult;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.Collection;
import java.util.stream.Stream;

@ApplicationScoped
public class RoleSearcher {
    private final RoleRepository roleRepository;
    private final UserRoleRepository userRoleRepository;

    @Inject
    public RoleSearcher(RoleRepository roleRepository,
                        UserRoleRepository userRoleRepository) {
        this.roleRepository = roleRepository;
        this.userRoleRepository = userRoleRepository;
    }

    public Ranged<RoleEntity> by(RoleQuery query) {
        return Ranged.of(roleRepository.by(query), query.getRange());
    }

    public Stream<UserRoleEntity> findByUserIds(Collection<String> userIds) {
        return userRoleRepository.findByUserIds(userIds);
    }
}
